package activator;

import java.util.Dictionary;
import java.util.Hashtable;

import library.service.Book;
import library.service.LibraryService;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

public class LibraryConsumerActivator implements BundleActivator {

	private static BundleContext context;
	private ServiceReference libraryServiceReference;

	private static String FILTER_TECHNICAL = "(category=technical)";
	private static String FILTER_FICTION = "(category=fiction)";

	private static void setContext(BundleContext context) {
		LibraryConsumerActivator.context = context;
	}

	// --- Implementation BundleActivator interface ----------------------------
	public void start(BundleContext context) throws Exception {
		// set bundle context
		setContext(context);

		ServiceReference[] serviceReferences = context.getServiceReferences(
				LibraryService.class.getName(), FILTER_FICTION);

		// check if there are ServiceReferences ( sr[] !=null)
		if (serviceReferences == null) {
			System.out.println("There are not registered services "
					+ "which satisfy the specified filter.");
			return;
		}
		libraryServiceReference = serviceReferences[0];

		// test LibraryService operations

		// 01 test - list
		testListBooksMethod();

		// 02 test - add
		String testBookName = getTestBookName();
		testAddBook(testBookName);

		// TODO finish test to other LibraryService operations
	}

	public void stop(BundleContext context) throws Exception {
		if (libraryServiceReference != null) {
			context.ungetService(libraryServiceReference);
			libraryServiceReference = null;

			System.out.println("LibraryService UNGET SERVICE");
		}
		// reset bundleContext to NULL
		setContext(null);
	}

	// --- Test Methods --------------------------------------------------------
	private void testAddBook(String bookName) {
		// load bookProperties
		Dictionary bookProperties = new Hashtable();
		bookProperties.put("name", bookName);

		// get LibraryService and test specific method
		LibraryService libraryService = getLibraryService();
		Book newBook = libraryService.addBook(bookProperties);
		System.out.println("Added New Book: " + newBook.toString());
		System.out.println("-------------------------");
	}

	private void testListBooksMethod() {
		LibraryService libraryService = getLibraryService();

		System.out.println("testListBooksMethod()");

		Book[] techBooks = libraryService.listBooks();
		StringBuffer strBuffer = new StringBuffer(30);
		strBuffer.append("Books: ");
		for (int i = 0; i < techBooks.length; i++) {
			strBuffer.append(techBooks[i].getName());
			if (i != techBooks.length - 1) {
				strBuffer.append(", ");
			}
		}
		System.out.println(strBuffer.toString());

		// logging info
		System.out.println("libraryService property 'category': "
				+ getCategory());
		System.out.println("-------------------------");
	}

	// --- Auxiliary methods ---------------------------------------------------
	private LibraryService getLibraryService() {
		LibraryService libraryService = (LibraryService) context
				.getService(libraryServiceReference);

		if (libraryService == null) {
			throw new IllegalArgumentException(
					"Null-pointed service in getting process.");
		}
		return libraryService;
	}

	private String getCategory() {
		return (String) libraryServiceReference.getProperty("category");
	}

	private String getTestBookName() {
		String bookName = null;
		if ("technical".equalsIgnoreCase(getCategory())) {
			bookName = "Delphi";
		}

		if ("fiction".equalsIgnoreCase(getCategory())) {
			bookName = "Shakespeare";
		}
		return bookName;
	}
}
